package com.shanghaionstar.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.CustomButton;
import com.shanghaionstar.R;

/**
 * Created by 7828wc on 16/7/6.
 */
public class CommonProgressButton extends LinearLayout {
    private View view;
    private CustomButton btn_common;
    private ProgressBar request_loading;

    public CommonProgressButton(Context context) {
        super(context);
        initView(null);
    }

    public CommonProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public CommonProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.common_progress_button, this);
        btn_common = (CustomButton) view.findViewById(R.id.btn_common);
        request_loading = (ProgressBar) view.findViewById(R.id.request_loading);
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, com.onstar.cn.weight.R.styleable.common_button);
            String text = typedArray.getString(com.onstar.cn.weight.R.styleable.common_button_text);
            if (!CommonUtil.isNull(text)) {
                btn_common.setText(text);
            }
            typedArray.recycle();
        }
    }

    public void setButtonBg(int resId) {
        btn_common.setBackgroundResource(resId);
    }
    public void setTextColor(int resId) {
        btn_common.setTextColor(this.getResources().getColor(resId));
    }

    public void setButtonEnabled(boolean enabled) {
        btn_common.setEnabled(enabled);
    }

    public boolean isButtonEnable(){
        return  btn_common.isEnabled();
    }

    public void showLoading(boolean isShow) {
        request_loading.setVisibility(isShow ? View.VISIBLE : View.GONE);
        btn_common.setEnabled(!isShow);
    }

    public void setText(String text) {
        if (!CommonUtil.isNull(text)) {
            btn_common.setText(text);
        }
    }

    public void setText(int resId) {
        setText(getContext().getString(resId));
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        btn_common.setOnClickListener(onClickListener);
    }

}
